<template>
    
</template>
<script>
export default {
    name: 'c_tile_b3dm',
    data() {
        return {
            viewer:null,
            tileset:null,
        }
    },
    mounted () {
        let t=this
        this.$nextTick(()=>{
            t.Cesium=t.$parent.Cesium
            t.viewer=t.$parent.viewer
            t.load3DTiles()
        })
    },
    props:{
        url:{
            type:String,
            default:'http://localhost:9911/Scene/Production_1.json'
        },
        alpha:{
            type:Number,
            default:1
        },
        aboveH:{
            type:Number,
            default:0
        }
    },
    watch:{
        alpha:{
			handler: function(newV,oldV) {
				if(!this.tileset)return
				// this.layers.forEach((lay)=>{ //s3m
				// 	lay.style3D.fillForeColor = new Cesium.Color(1.0, 1.0, 1.0,newV)
				// })
                this.tileset.style=new this.Cesium.Cesium3DTileStyle({
                    color: `rgba(255, 255,255, ${this.alpha})`,
                })
			},
		},
        immediate:true
    },
    methods:{
        flyToEntry(){
            this.viewer.camera.flyTo(
                {"destination":{"x":-2675302.0846455884,"y":5006577.988229876,"z":2907567.4330674578},
                "orientation":{"heading":6.244713349521609,"pitch":-1.2131977999445378,"roll":6.282883046111946},
                duration:1})
        },
        load3DTiles(){
            let t=this
            let {Cesium}=t  
            let tileset= this.tileset = new Cesium.Cesium3DTileset({
                url:this.url,
                maximumScreenSpaceError: 1,
            });
            t.viewer.scene.primitives.add(tileset);
            // t.viewer.zoomTo(tileset);
            t.flyToEntry()
            tileset.readyPromise.then(function(tileset) {
                var heightOffset = t.aboveH;  //设置建筑物与地面的高度
                var boundingSphere = tileset.boundingSphere;
                var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
                var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
                var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, heightOffset);
                var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
                tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
                // t.viewer.zoomTo(tileset);//删除
            });
        },
    }

}
</script>